package dman;

import au.com.bytecode.opencsv.CSVReader;
import au.com.bytecode.opencsv.CSVWriter;
import dman.VIN_DB.VINrecord;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;

/**
 * Class contain records and methods for VINs description
 */
public class VIN_Desc {

    public ArrayList ModelList, MakeList;
    public ArrayList VIN_Desc;

    public class VIN_details {
        public String VIN;
        public String Model;
        public String Make;
        public String Year;
    }

    public VIN_details newRecord () {
        return new VIN_details();
    }

    VIN_Desc (String VIN_FILE) {
        VIN_Desc=new ArrayList ();
        CSVReader reader;
        try {
            reader = new CSVReader(new FileReader(VIN_FILE));
    		String [] nextLine;
            try {
                while ((nextLine = reader.readNext()) != null) {
                    VIN_details record = new VIN_details();
                    record.VIN=nextLine[0].trim();
                    record.Make=nextLine[1].trim();
                    record.Model=nextLine[2].trim();
                    record.Year=nextLine[3].trim();
                    VIN_Desc.add (record);
                }
                reader.close();
            } catch (IOException ex) {

                 System.out.println ("Error: "+ex.getMessage());
            }
        }
        catch (FileNotFoundException ex) {
            System.out.println ("Error: "+ex.getMessage());
        }

        MakeList=new ArrayList();
        ModelList=new ArrayList ();

        Iterator itr = VIN_Desc.iterator();
        while (itr.hasNext()) {
            VIN_details record=(VIN_details) itr.next();
            AddItemIfUnique (record.Model,ModelList);
            AddItemIfUnique (record.Make, MakeList );
        }
    }

    public boolean AddItemIfUnique (String Model, ArrayList al) {
        boolean isPresent = false;
        Iterator itr = al.iterator();
        while (itr.hasNext()) {
            String s = (String) itr.next();
            if (s.equalsIgnoreCase(Model)) {
               isPresent = true;
               break;
               }
            }
        if (!isPresent)
            al.add(Model);
        return !isPresent;
    }

    public boolean AddVINIfUnique(VIN_details record){
        boolean isPresent = false;
        Iterator itr = VIN_Desc.iterator();
        VIN_details details;
        while (itr.hasNext()) {
            details= (VIN_details) itr.next();
            if (details.VIN.equalsIgnoreCase(record.VIN)) {
               isPresent = true;
               break;
               }
            }
        if (!isPresent) {
            VIN_Desc.add(record);
            }
        return !isPresent;
    }

    public void SaveDesc (String DB_FILE) {
        try {
            CSVWriter writer = new CSVWriter(new FileWriter(DB_FILE));
            VIN_details record;
            for (int i = 0; i < VIN_Desc.size(); i++) {
                record = (VIN_details) VIN_Desc.get(i);
                    String[] entries = new String [4];
                    entries[0]=record.VIN;
                    entries[1]=record.Make;
                    entries[2]=record.Model;
                    entries[3]=record.Year;
                    writer.writeNext(entries);
            }
            writer.close();
        } catch (IOException ex) {
            System.out.println("Error: "+ex.getMessage());
        }
    }

    public VIN_details getDetailsRecordByVIN (String fVIN) {
        VIN_details res = null;
        VIN_details record;
        for (int i=0; i<VIN_Desc.size(); i++){
          record=(VIN_details)VIN_Desc.get(i);
          //LogText ("comparing: "+FileName+" to: "+record.FileName);
          if (fVIN.equalsIgnoreCase(record.VIN)) {
             res=record;
             //LogText ("found: "+FName);
             break;
          }
        }
        return res;
    }
}
